#include "stream.h"

int main(int argc, char **argv)
{
  ctrl_str 	ctrl;
  double 	dt, tmax, t_out;
  int 		j,k,q;
  char		fname[80];
  double	c1, c2, c3, c4;
  double	d1, d2, d3, d4;

  d1 = 1 / ( 2 - pow(2, 1/3.) );
  d3 = d1;
  d2 = 1 - (d1+d3);

  c1 = 0.5 * d1;
  c2 = 0.5 - c1;
  c3 = 0.5 - c1;
  c4 = c1;

  
  load_params(argv, &ctrl);
  j = 0; q = 0; tmax = ctrl.t_max; t_out = 0.;
  if ((ctrl.ly/ctrl.ny) > (ctrl.lx/ctrl.nx)) dt = (ctrl.cfl)*(ctrl.lx/ctrl.nx);
  else dt = (ctrl.cfl)*(ctrl.ly/ctrl.ny);  
  init_fft(&ctrl);
  load_text(&ctrl);
  printf("%s\n", ctrl.runname);
  if (ctrl.zero == 1) {
    generate_ic(&ctrl);
    printf("Setting IC to zero\n");
  }
  compute_velocity(&ctrl);
  compute_gradv(&ctrl);
  save_text(&ctrl, "easy.out");
  //printf("Debug:\t FFlag = %f\n",ctrl.FFlag);
  while (j*dt < tmax) {    
    if (t_out < j*dt){
      sprintf(fname,"%s_%04d.out", ctrl.runname, q);
      printf("current time %f\t outfile %s\n", t_out, fname);
      save_text(&ctrl, fname);
      t_out = t_out + ctrl.tskip;
      q++;
    }

    rk4(&ctrl,1.);
    /*rk4_nl(&ctrl,0.5);
    diffusion_step(&ctrl,1.);
    rk4_nl(&ctrl,0.5);*/

    /*diffusion_step(&ctrl,c1);
    rk4_nl(&ctrl,d1);
    diffusion_step(&ctrl,c2);
    rk4_nl(&ctrl,d2);
    diffusion_step(&ctrl,c3);
    rk4_nl(&ctrl,d3);
    diffusion_step(&ctrl,c4);*/

    j++;
  }
  sprintf(fname,"%s_final.out", ctrl.runname);
  printf("# of iteration %d\t outfile %s\n", j, fname);
  save_text(&ctrl, fname);


  printf("Done\n"); 
  return 0;
}
